package com.stanleylam.filemanager;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.ListActivity;
import android.os.Bundle;
import android.os.SystemClock;
import android.text.format.DateFormat;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

public class MainActivity extends ListActivity {
	public static final String TAG = "MainActivity";

	private static final String FILENAME = "filename";
	private static final String DETAIL = "detail";
	private static final File FILE_ROOT = File.listRoots()[0];
	private File mCurrentFile;
	private long mConfirmExitTime;

	private final List<File> mFileList = new ArrayList<File>();
	private final List<Map<String, String>> mList = new ArrayList<Map<String, String>>();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		SimpleAdapter adapter = new SimpleAdapter(this, mList,
				android.R.layout.simple_list_item_2, new String[] { FILENAME,
						DETAIL }, new int[] { android.R.id.text1,
						android.R.id.text2 });
		setListAdapter(adapter);
		getListView().setOnItemClickListener(
				new AdapterView.OnItemClickListener() {
					@Override
					public void onItemClick(AdapterView<?> parent, View view,
							int position, long id) {
						File file = mFileList.get(position);
						if (file.isDirectory()) {
							mCurrentFile = file;
							getFileList();
						}
					}
				});

		mCurrentFile = FILE_ROOT;
		getFileList();
	}

	@Override
	public void onBackPressed() {
		if (SystemClock.uptimeMillis() - mConfirmExitTime <= 2000L) { // LENGTH_SHORT
			finish();
		}

		File parent = mCurrentFile.getParentFile();

		if (parent == null) {
			confirmExit();
		} else {
			mCurrentFile = parent;
			getFileList();
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.item1:
			Log.d(TAG, "item1");
			return true;
		case R.id.item2:
			Log.d(TAG, "item2");
			return true;
		default:
			return false;
		}
	}

	private void getFileList() {
		mList.clear();
		mFileList.clear();
		File[] filesInDirectory = mCurrentFile.listFiles();
		for (File f : filesInDirectory) {
			Map<String, String> map = new HashMap<String, String>(2);
			map.put(FILENAME, f.getName());
			map.put(DETAIL,
					DateFormat.format("yyyy MM dd h:mmaa", f.lastModified())
							.toString());
			mList.add(map);
			mFileList.add(f);
		}
		((SimpleAdapter) getListAdapter()).notifyDataSetChanged();
	}

	private void confirmExit() {
		mConfirmExitTime = SystemClock.uptimeMillis();
		Toast.makeText(this, R.string.confirmExit, Toast.LENGTH_SHORT).show();
	}
}